package com.zbkj.service.service.impl;

import cn.hutool.core.date.DateUtil;
import com.zbkj.common.request.PageParamRequest;
import com.github.pagehelper.PageHelper;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zbkj.common.exception.AppException;

import com.zbkj.common.request.ZfbDeviceMatchLogSaveRequest;
import com.zbkj.common.request.ZfbDeviceMatchLogSearchRequest;
import com.zbkj.common.response.ZfbDeviceMatchLogPageDataResponse;
import com.zbkj.common.result.CommonResultCode;
import com.zbkj.common.utils.CrmebDateUtil;
import com.zbkj.common.model.traceable.ZfbDeviceMatchLog;
import com.zbkj.service.dao.ZfbDeviceMatchLogDao;
import com.zbkj.service.service.ZfbDeviceMatchLogService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author dazongzi
* @description ZfbDeviceMatchLogServiceImpl 接口实现
* @date 2025-06-12
*/
@Service
public class ZfbDeviceMatchLogServiceImpl extends ServiceImpl<ZfbDeviceMatchLogDao, ZfbDeviceMatchLog> implements ZfbDeviceMatchLogService {

    @Resource
    private ZfbDeviceMatchLogDao dao;


    @Override
    public Boolean create(ZfbDeviceMatchLogSaveRequest zfbDeviceMatchLogSaveRequest) {
        ZfbDeviceMatchLog zfbDeviceMatchLog = new ZfbDeviceMatchLog();
        BeanUtils.copyProperties(zfbDeviceMatchLogSaveRequest, zfbDeviceMatchLog, "id");
        boolean save = save(zfbDeviceMatchLog);
        if (!save) {
            throw new AppException(CommonResultCode.VALIDATE_FAILED, "添加失败");
        }
        return save;
    }

    /**
     * 分页查询设备匹配日志表
     *
     * @param request          请求参数
     * @param pageParamRequest 分页类参数
     * @return List<ZfbDeviceMatchLog>
     * @author dazongzi
     * @since 2025-06-12
     */
    @Override
    public List<ZfbDeviceMatchLogPageDataResponse> getList(ZfbDeviceMatchLogSearchRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());

        Date currentDate = CrmebDateUtil.nowDateTime();

        //带 ZfbDeviceMatchLog 类的多条件查询
        LambdaQueryWrapper<ZfbDeviceMatchLog> lambdaQueryWrapper = Wrappers.lambdaQuery();
        // type 类型不能为空 bean中已经限制

//        lambdaQueryWrapper.eq(ZfbDeviceMatchLog::getType, request.getType());
//
//        if (ObjectUtil.isNotEmpty(request.getId()) && request.getId() > 0) {
//            lambdaQueryWrapper.eq(ZfbDeviceMatchLog::getId, request.getId());
//        }
//        if (ObjectUtil.isNotEmpty(request.getName())) {
//            lambdaQueryWrapper.like(ZfbDeviceMatchLog::getName, URLUtil.decode(request.getName()));
//        }
//
//        if (ObjectUtil.isNotEmpty(request.getStatus())) {
//            lambdaQueryWrapper.eq(ZfbDeviceMatchLog::getStatus, request.getStatus());
//        }
//        if (ObjectUtil.isNotEmpty(request.getMethod())) {
//            lambdaQueryWrapper.eq(ZfbDeviceMatchLog::getMethod, request.getMethod());
//        }

        // 时间区间
        //if (ObjectUtil.isNotEmpty(request.getStarttime()) && ObjectUtil.isNotEmpty(request.getEndtime())) {
        //    lambdaQueryWrapper.between(ZfbDeviceMatchLog::getCreatetime, request.getStarttime(), request.getEndtime());
        //}
        // * -1 已结束 结束时间小于当前时间
        // * 1 进行中 结束时间大于当前时间 && 开始时间小于等于当前时间
        // * 0 未开始 开始时间大于当前时间
//        if (ObjectUtil.isNotEmpty(request.getRunningStatus())) {
//            switch (request.getRunningStatus()) {
//                case -1:
//                    lambdaQueryWrapper.lt(ZfbDeviceMatchLog::getEndtime, currentDate);
//                    break;
//                case 0:
//                    lambdaQueryWrapper.gt(ZfbDeviceMatchLog::getStarttime, currentDate);
//                    break;
//                case 1:
//                    lambdaQueryWrapper.ge(ZfbDeviceMatchLog::getEndtime, currentDate).le(ZfbDeviceMatchLog::getStarttime, currentDate);
//                    break;
//            }
//        }

        lambdaQueryWrapper.orderByDesc(ZfbDeviceMatchLog::getCreatedAt);
        List<ZfbDeviceMatchLog> zfbDeviceMatchLogs = dao.selectList(lambdaQueryWrapper);
        // 添加活动状态
        List<ZfbDeviceMatchLogPageDataResponse> zfbDeviceMatchLogResponses = new ArrayList<>();

        zfbDeviceMatchLogs.stream().forEach(x->{
            ZfbDeviceMatchLogPageDataResponse zfbDeviceMatchLogResponse = new ZfbDeviceMatchLogPageDataResponse();
            BeanUtils.copyProperties(x, zfbDeviceMatchLogResponse);
//            zfbDeviceMatchLogResponse.setRunningStatus(x.getStarttime(), x.getEndtime(), currentDate);
            zfbDeviceMatchLogResponses.add(zfbDeviceMatchLogResponse);
        });
        return zfbDeviceMatchLogResponses;
    }

    @Override
    public ZfbDeviceMatchLog getLatstByImeiMac(String imei, String mac) {
        return getOne(new LambdaQueryWrapper<ZfbDeviceMatchLog>()
                .eq(ZfbDeviceMatchLog::getImei, imei)
                .eq(ZfbDeviceMatchLog::getDeviceMac, mac)
                .orderByDesc(ZfbDeviceMatchLog::getRefreshTime)
                .last("limit 1")
        );
    }

    @Override
    public List<ZfbDeviceMatchLog> getList(Integer bindId, List<String> macList) {
        return list(new LambdaQueryWrapper<ZfbDeviceMatchLog>()
                .eq(ZfbDeviceMatchLog::getDeviceMac, macList)
                .eq(ZfbDeviceMatchLog::getBindId, bindId)
                .le(ZfbDeviceMatchLog::getExpTime, new Date())
                .orderByDesc(ZfbDeviceMatchLog::getRefreshTime)
        );
    }

    @Override
    public List<ZfbDeviceMatchLog> getListByMacList(List<String> recMacList) {
        return list(new LambdaQueryWrapper<ZfbDeviceMatchLog>()
                .eq(ZfbDeviceMatchLog::getDeviceMac, recMacList)
                .le(ZfbDeviceMatchLog::getExpTime, new Date())
                .orderByDesc(ZfbDeviceMatchLog::getRefreshTime)
        );
    }

    @Override
    public List<ZfbDeviceMatchLog> getListByBindId(List<Integer> bindIdList) {
        return list(new LambdaQueryWrapper<ZfbDeviceMatchLog>()
                .in(ZfbDeviceMatchLog::getBindId, bindIdList)
                .gt(ZfbDeviceMatchLog::getExpTime, DateUtil.now())
                .orderByDesc(ZfbDeviceMatchLog::getRefreshTime)
        );
    }


}

